package com.geek.jdbc.utils;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
import java.sql.*;

public class JDBCUtil {
    //获取连接
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdql?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai",
                    "root", "root");
            System.out.println("连接成功");
            return conn;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //关闭资源
    public static void close(Connection conn  , Statement stmt , ResultSet rs){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
